
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class BomberGraphics {

    private int size;
    private int w,  h;
    private Graphics bg;
    private Image man[][];
    private Image brick,  box;
    private Image[] bomb;
    private Image[][] boom;
    private Image[] boomBrick;
    private Image[] pu;
    private final Color COLOR = Color.YELLOW;
    private String host;
    
    public BomberGraphics(String host, int w, int h, int size) {
        this.host = host;
        this.w = w;
        this.h = h;
        this.size = size;
        download("bg.png");
    }

    private Image reSize(Image img) {
        int w = img.getWidth(null) * size;
        int h = img.getHeight(null) * size;
        BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
        b.getGraphics().drawImage(img, 0, 0, w, h, null);
        return b;
    }

    private Image flipV(Image img) {
        BufferedImage b = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        b.getGraphics().drawImage(img,
                img.getWidth(null), 0, 0, img.getHeight(null),
                0, 0, img.getWidth(null), img.getHeight(null),
                null);
        return b;
    }

    public Image flipH(Image img) {
        BufferedImage b = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        b.getGraphics().drawImage(img,
                0, img.getHeight(null), img.getWidth(null), 0,
                0, 0, img.getWidth(null), img.getHeight(null),
                null);
        return b;
    }

    public Image rotate(Image img) {
        BufferedImage b = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        double x = 0.5 * b.getWidth();
        double y = 0.5 * b.getHeight();
        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(90), x, y);
        Graphics2D g2 = (Graphics2D) b.getGraphics();
        g2.transform(at);
        g2.drawImage(img, 0, 0, null);
        return b;
    }

    public BufferedImage drawMap(Integer[][] walk) {
        BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g = b.getGraphics();
        g.setColor(COLOR);
        g.fillRect(0, 0, w, h);
        for (int i = 0; i < walk.length; i++) {
            for (int j = 0; j < walk[i].length; j++) {
                if (walk[i][j] == 1) {
                    g.drawImage(box, i * 16 * size, j * 16 * size, null);
                } else if (walk[i][j] == 2) {
                    g.drawImage(brick, i * 16 * size, j * 16 * size, null);
                }
            }
        }
        return b;
    }

    public Image clearRect(Image img, int x, int y) {
        Graphics g = img.getGraphics();
        g.setColor(COLOR);
        g.fillRect(x, y, 16 * size, 16 * size);
        return img;
    }

    public Image download(
            String name) {
        BufferedImage b = null;
        try {
            b = ImageIO.read(new URL("http://" + host + "/bomberman/" + name));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return b;
    }

    public Image man(int i, int j) {
        return man[i][j];
    }

    public Image bomb(
            int i) {
        return bomb[i];
    }

    public Image boom(
            int i, int j) {
        return boom[i][j];
    }

    public Image boomBrick(
            int i) {
        return boomBrick[i];
    }
    
    public Image pu(int i) {
        return pu[i];
    }

    public void load() {
        man = new Image[4][4];
        man[0][0] = download("ml1.png");
        man[0][1] = download("ml2.png");
        man[0][2] = man[0][0];
        man[0][3] = download("ml3.png");

        man[1][0] = download("mu1.png");
        man[1][1] = download("mu2.png");
        man[1][2] = man[1][0];
        man[1][3] = flipV(man[1][1]);

        man[2][0] = flipV(man[0][0]);
        man[2][1] = flipV(man[0][1]);
        man[2][2] = man[2][0];
        man[2][3] = flipV(man[0][3]);

        man[3][0] = download("md1.png");
        man[3][1] = download("md2.png");
        man[3][2] = man[3][0];
        man[3][3] = flipV(man[3][1]);

        if (size != 1) {
            for (int i = 0; i <
                    man.length; i++) {
                for (int j = 0; j <
                        man[i].length; j++) {
                    man[i][j] = reSize(man[i][j]);
                }

            }
        }


        brick = reSize(download("brick.png"));
        box =
                reSize(download("box.png"));
        bomb =
                new Image[3];
        bomb[0] = reSize(download("bomb1.png"));
        bomb[1] = reSize(download("bomb2.png"));
        bomb[2] = reSize(download("bomb3.png"));

        boom =
                new Image[4][7];
        for (int i = 0; i <
                4; i++) {
            boom[i][0] = download("boomcore" + (i + 1) + ".png");
            boom[i][1] = download("boomside" + (i + 1) + ".png");
            boom[i][2] = rotate(boom[i][1]);
            boom[i][3] = download("boomend" + (i + 1) + ".png");
            boom[i][4] = rotate(boom[i][3]);
            boom[i][5] = flipV(boom[i][3]);
            boom[i][6] = flipH(boom[i][4]);
        }

        for (int i = 0; i <
                boom.length; i++) {
            for (int j = 0; j <
                    boom[i].length; j++) {
                boom[i][j] = reSize(boom[i][j]);
            }

        }

        boomBrick = new Image[2];
        boomBrick[0] = reSize(download("boombrick1.png"));
        boomBrick[1] = reSize(download("boombrick2.png"));
        
        pu = new Image[1];
        for (int i = 0; i < pu.length; i++) {
            pu[i] = reSize(download("pu" + (i + 1) + ".png"));
        }
    }
}
